package com.condroid;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import ioio.lib.api.Uart;


/**
 * This is Setting class for setting up UART or serial
 * communication and it contains different like set parity 
 * etc methods to be invoked by other classes related to 
 * settings class.
 * @author Yasir Ali & Jun gao
 * Condroid Summer CSD 2011 - KTH
 */
public class Settings {
	public static String portRcv;
	public static String baud;
	public static Uart.StopBits stopBits;
	public static Uart.Parity parity;
	public static SharedPreferences newSettingData;
	public static SharedPreferences.Editor	Parity;
	public static SharedPreferences.Editor  StopBits;
	public static SharedPreferences.Editor  Baud;
	public static SharedPreferences.Editor  PortTransmit;
	public static SharedPreferences.Editor  PortReceive;
	private static String temp;
	private static String portTrans;
	
	public Settings(){
		
	}
	
	
	
	
	/***/
	
	/**
	 * Set PARITY for UART serial communication with the device and saves in the storage
	 * so that the other classes can use it.
	 * @param pr
	 * @return boolean
	 */
	public static boolean setparity(String pr) {
			
			if(pr.equals("NONE")){
				Parity=newSettingData.edit();
				Parity.putString("Parity1", pr);
				Parity.commit();
				return true;
			}
			else if(pr.equals("EVENING")){
				Parity=newSettingData.edit();
				Parity.putString("Parity1", pr);
				Parity.commit();
				return true;
			}
			else if(pr.equals("ODD")){
				Parity=newSettingData.edit();
				Parity.putString("Parity1", pr);
				Parity.commit();
				return true;
			}
			
		return false;
	}
	
	
	/***/
	/**
	 * Set STOPBITS for UART serial communication with the device and saves in the storage
	 * so that the other classes can use it.
	 * @param sb
	 * @return boolean
	 */
	public static boolean setstopBits(String sb) {
		if(sb.equals("ONE")){
			StopBits=newSettingData.edit();
			StopBits.putString("StBit1", sb);
			StopBits.commit();
			return true;
		}
		else if(sb.equals("TWO")){
			StopBits=newSettingData.edit();
			StopBits.putString("StBit1", sb);
			StopBits.commit();
			return true;
		}
		
			
	return false;		
	}
	
	/***/

	/**
	 * Set BAUD for UART serial communication with the device and saves in the storage
	 * so that the other classes can use it.
	 * @param b
	 * @return
	 */
	public static boolean setbaud(String b) {
		if(b.equals("2400") |b.equals("4800") |b.equals("9600") |b.equals("19200") | 
				b.equals("38400") |b.equals("57600") |b.equals("115200") |b.equals("31250")){
			Baud=newSettingData.edit();
			Baud.putString("Baud1", b);
			Baud.commit();
			return true;
			}
		return false;
		
	}
	/***/
	
	/**
	 * Set TRANSPORT for UART serial communication with the device and saves in the storage
	 * so that the other classes can use it.
	 * @param pt
	 * @return boolean
	 */
	public static boolean setTransPort(String pt) {
		if(pt.equals("3") |pt.equals("4") |pt.equals("5") |pt.equals("6") ){
			PortTransmit=newSettingData.edit();
			PortTransmit.putString("PortT1", pt);
			PortTransmit.commit();
			return true;
			}
		return false;	
	}
	
	/***/
	
	/**
	 * Set RCVPORT for UART serial communication with the device and saves in the storage
	 * so that the other classes can use it.
	 * @param pr
	 * @return boolean
	 */
	public static boolean setRcvPort(String pr) {
		if(pr.equals("10") |pr.equals("11") |pr.equals("12") |pr.equals("13") ){
			PortReceive=newSettingData.edit();
			PortReceive.putString("PortR1", pr);
			PortReceive.commit();
			return true;
			}
		return false;	
	}

	/***/
	
	/**
	 * Get PARITY for UART serial communication with the device and saves in the storage
	 * so that the other classes can use it.
	 * @return UART instance of parity
	 */
	public static Uart.Parity getparity() {
		temp=newSettingData.getString("Parity1", "Coudnt load Data!!");
		if(temp.equals("NONE")){
			parity = Uart.Parity.NONE;
			return parity;
		}
		else if(temp.equals("EVEN")){
			parity = Uart.Parity.EVEN;
			return parity;
		}
		else if(temp.equals("ODD")){
			parity = Uart.Parity.ODD;
			return parity;
		}
		
		return null;
	}
	
	/***/
	
	/**
	 * Get STOPBITS for UART serial communication with the device and saves in the storage
	 * so that the other classes can use it.
	 * @return UART instance of Stopbits
	 */
	public static  Uart.StopBits getstopBits() {
		temp=newSettingData.getString("StBit1", "Coudnt load Data!!");
		if(temp.equals("ONE")){
			stopBits = Uart.StopBits.ONE;
			return stopBits;
		}
		else if(temp.equals("TWO")){
			stopBits = Uart.StopBits.TWO;
			return stopBits;
		}
		return null;
			
	}
	
	/***/
	
	/**
	 * GET BAUD for UART serial communication with the device and saves in the storage
	 * so that the other classes can use it.
	 * @return
	 */
	public static int getbaud() {
		baud=newSettingData.getString("Baud1","Coundnt load Data");
		if(baud!=null){
			return Integer.parseInt(baud);
		}
		return 0;
		
	}
	/***/
	
	/**
	 * Get GetTransPort for UART serial communication with the device and saves in the storage
	 * so that the other classes can use it.
	 * @return int
	 */
	public static int getTransPort() {
		
		portTrans=newSettingData.getString("PortT1","Coundnt load Data");
		if(portTrans!=null){
			return Integer.parseInt(portTrans);
		}
		return 0;
		
	}
	/***/
	
	/**
	 * Get getRcvPort for UART serial communication with the device and saves in the storage
	 * so that the other classes can use it.
	 * @return int
	 */
	public static int getRcvPort() {
		portRcv=newSettingData.getString("PortR1","Coundnt load Data");
		if(portRcv!=null){
			return Integer.parseInt(portRcv);
		}
		return 0;		
	}
	
	/**
	 * Give privileges to other classes and activity to use these shared 
	 * preferences (!!!!This method needs to be called in order to use any
	 * of the methods in this class!!!!).
	 * @param context
	 * @return
	 */
	
	public static void getConfiguration(Context context){
		
		newSettingData = PreferenceManager.getDefaultSharedPreferences(context);
		newSettingData = context.getSharedPreferences(SettingsActivity.filename,0);
//		temp=settingData.getString("Parity2", "Not able to Load");
	}	

	
	
	
}
